package com.threeti.mecool.core.infrastructure.persistence;

import com.threeti.mecool.core.domain.model.Memo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface MemoRepository extends JpaRepository<Memo, Long>, JpaSpecificationExecutor<Memo> {
    Page<Memo> findByUser_LoginName(String userLoginName, Pageable pageable);

    @Modifying
    @Transactional
    @Query("delete Memo where id = ?1")
    int deleteWithRtnCntBy(Long memoId);

    @Query("select m from Memo m inner join m.channel mc where m.user.loginName = ?1 and mc.code = ?2 order by m.recordDate")
    List<Memo> findByUserIdAndChannelCodeOrderByRecordDate(String loginName, String channelCode);
}
